package com.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.cloud.admin.common.core.domain.AjaxResult;
import com.cloud.admin.common.core.page.TableDataInfo;
import com.cloud.admin.model.entity.SysDictData;
import com.cloud.admin.service.SysDictDataService;
import com.cloud.admin.shiro.security.ShiroUtils;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * Created on 2019/7/25.
 *
 * @author
 */
@RestController
@AllArgsConstructor
@RequestMapping("system/dict/data")
public class DictDataController extends BaseController {
    private final String prefix = "system/dict/data/";

    private final SysDictDataService dictDataService;

    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public ModelAndView dictData() {
        ModelAndView view = super.baseView("data", prefix + "data");
        return view;
    }

    @PostMapping("list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(SysDictData dictData, Page page) {
        IPage pageData = dictDataService.selectPageByLike(page, dictData);
        return getDataTable(pageData);
    }

    /**
     * 新增字典类型
     */
    @GetMapping("add/{dictType}")
    public ModelAndView add(@PathVariable("dictType") String dictType) {
        ModelAndView view = super.baseView("add", prefix + "add");
        view.addObject("dictType", dictType);
        return view;
    }

    /**
     * 新增保存字典类型
     */
    @RequiresPermissions("system:dict:add")
    @PostMapping("add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysDictData dict) {
        dict.setCreateBy(ShiroUtils.getLoginName());
        dict.setCreateTime(new Date());
        return toAjax(dictDataService.save(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("edit/{dictCode}")
    public ModelAndView edit(@PathVariable("dictCode") Long dictCode) {
        ModelAndView view = super.baseView("edit", prefix + "edit");
        view.addObject("dict", dictDataService.getById(dictCode));
        return view;
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysDictData dict) {
        dict.setUpdateBy(ShiroUtils.getLoginName());
        dict.setUpdateTime(new Date());
        return toAjax(dictDataService.updateById(dict));
    }

    @RequiresPermissions("system:dict:remove")
    @PostMapping("remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dictDataService.removeByIds(Arrays.stream(ids.split(",")).collect(Collectors.toList())));
    }
}
